package main.java.com.study.设计模式.單例.study实现Map单例;

import java.util.HashMap;
import java.util.Map;

/**
 * map的單例模式
 * 線程不安全的
 */
public class MyMap {

    private Map<Integer, Object> map = new HashMap<Integer, Object>();
    private Object lock = new Object();

    private static MyMap instance = new MyMap();

    private MyMap() {}

    // 加上synchronized就是線程安全的了，但是效率低。
    public  static MyMap getInstance() {
        if (instance == null) {
            instance = new MyMap();
        }
        return instance;
    }


    public void put(Integer taskId, String name) {
        synchronized (lock) {
            map.put(taskId, name);
        }
    }

    public Map<Integer, Object> getMap() {
        return map;
    }

}